using CfgTable;
using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;

namespace IQIGame.Onigao.GamePlay
{
    public class UIHeroUpdatePop : UIBaseWindow
    {
        /// <summary>
        /// 静态配置
        /// </summary>
        private static UICreateConfig _createConfig = new()
        {
            prefabName = nameof(UIHeroUpdatePop),
            layer = EUILayer.Normal,
        };

        /// <summary>
        /// 创建UI的配置
        /// </summary>
        public override IUICreateConfig createConfig => _createConfig;

        #region Template Generate,don't modify
        protected partial class UIB_UIHeroUpdatePop
        {
            #region ObjectBinding Generate
            public IQIGame.Onigao.Framework.ExButton btnClose { protected set; get; }
            public IQIGame.Onigao.Framework.ExText lvValue { protected set; get; }
            public IQIGame.Onigao.Framework.ExText lvNextValue { protected set; get; }
            public UnityEngine.GameObject gobjLevelCap { protected set; get; }
            public UnityEngine.GameObject gobjLevelUp { protected set; get; }
            public UnityEngine.GameObject gobjAattributeTip { protected set; get; }
            public UnityEngine.GameObject gobjAttribute { protected set; get; }
            public UnityEngine.GameObject gobjUnlockSkill { protected set; get; }
            public UnityEngine.GameObject gobjUnlockSkin { protected set; get; }
            public IQIGame.Onigao.Framework.ExImage iconSkill { protected set; get; }
            public IQIGame.Onigao.Framework.ExImage iconSkin { protected set; get; }
            public IQIGame.Onigao.Framework.ExText textSkil { protected set; get; }
            public IQIGame.Onigao.Framework.ExText textSkin { protected set; get; }
            public virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExButton>("btnClose", out var __tbv0);
                this.btnClose = __tbv0;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExText>("lvValue", out var __tbv1);
                this.lvValue = __tbv1;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExText>("lvNextValue", out var __tbv2);
                this.lvNextValue = __tbv2;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("gobjLevelCap", out var __tbv3);
                this.gobjLevelCap = __tbv3;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("gobjLevelUp", out var __tbv4);
                this.gobjLevelUp = __tbv4;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("gobjAattributeTip", out var __tbv5);
                this.gobjAattributeTip = __tbv5;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("gobjAttribute", out var __tbv6);
                this.gobjAttribute = __tbv6;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("gobjUnlockSkill", out var __tbv7);
                this.gobjUnlockSkill = __tbv7;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("gobjUnlockSkin", out var __tbv8);
                this.gobjUnlockSkin = __tbv8;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExImage>("iconSkill", out var __tbv9);
                this.iconSkill = __tbv9;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExImage>("iconSkin", out var __tbv10);
                this.iconSkin = __tbv10;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExText>("textSkil", out var __tbv11);
                this.textSkil = __tbv11;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExText>("textSkin", out var __tbv12);
                this.textSkin = __tbv12;
            }
            #endregion ObjectBinding Generate 
        }
        #endregion Template Generate,don't modify

        private ListScrollAdapter<UIN_HeroUpLevelAttribute> upAttribues;

        protected UIB_UIHeroUpdatePop ui { get; set; }

        private UD_HeroUpLv uiData => _data as UD_HeroUpLv;

        protected override void BeforeInit()
        {
            ui = new UIB_UIHeroUpdatePop();
            ui.InitBinding(this.csObjBind);
        }

        protected override void OnInit()
        {
            this.upAttribues = this.InitListScroll<UIN_HeroUpLevelAttribute>(this.ui.gobjAttribute, this.OnAtributeChanged);
            this.ui.btnClose.onClick.AddListener(this.Close);
        }

        protected override void OnShow(UIBaseData data)
        {
            if (uiData == null)
                return;
            ShowCommon();

            if (uiData.isUpAwaken)
                ShowUpHeroAwakenLevel();
            else
                ShowUpHeroLevel();
        }

        protected override void OnHide()
        {

        }

        protected override void OnDispose()
        {

        }

        private void ShowCommon()
        {
            int lastLv = uiData.isUpAwaken ? uiData.heroData.level : uiData.lastLv;
            int currLv = uiData.isUpAwaken ? uiData.heroData.maxLv : uiData.heroData.level;
            this.ui.lvValue.text = lastLv.ToString();
            this.ui.lvNextValue.text = currLv.ToString();

            this.ui.gobjLevelCap.SetActive(uiData.isUpAwaken);
            this.ui.gobjLevelUp.SetActive(!uiData.isUpAwaken);

            this.ui.gobjAattributeTip.SetActive(false);
            this.ui.gobjUnlockSkill.SetActive(false);
            this.ui.gobjUnlockSkin.SetActive(false);
        }

        private void ShowUpHeroLevel()
        {
            int cnt = APIUIHeroInfo.UpLevelShowAttributeIds.Length;
            this.upAttribues.RefillCells(cnt);
            this.ui.gobjAattributeTip.SetActive(true);
        }

        private void OnAtributeChanged(UIN_HeroUpLevelAttribute item, int index)
        {
            int cid = APIUIHeroInfo.UpLevelShowAttributeIds[index];
            int last = uiData.GetAttributeValue(index);
            int current = uiData.heroData.GetAttr(cid);
            item.Show(cid, last, current);
        }

        private void ShowUpHeroAwakenLevel()
        {
            CfgHeroAwaken cfgAwaken = uiData.heroData.cfgAwaken;
            ShowUnlockSkin(cfgAwaken);
            ShowUnlockSkill(cfgAwaken);
        }

        private void ShowUnlockSkin(CfgHeroAwaken cfgAwaken)
        {
            int cid = cfgAwaken.UnlockHeroSkin;
            bool isActive = cid > 0;
            if (isActive)
            {
                var cfgSkin = TableCenter.heroSkin.Get(cid);
                this.SetImageSprite(this.ui.iconSkin, cfgSkin.HeroHeadImage_fullPath);
                this.ui.textSkin.text = cfgSkin.Name;
            }
            this.ui.gobjUnlockSkin.SetActive(isActive);
        }

        private void ShowUnlockSkill(CfgHeroAwaken cfgAwaken)
        {
            // TODO - 解锁技能
            int cid = cfgAwaken.UnlockHomelandSkillID;
            bool isActive = false;// cid > 0;
            if (isActive)
            {
                // var cfgSkin = TableCenter.heroSkin.Get(cid);
                // this.SetImageSprite(this.ui.iconSkill, cfgSkin.HeroHeadImage_fullPath);
                // this.ui.textSkil.text = cfgSkin.Name;
            }
            this.ui.gobjUnlockSkin.SetActive(isActive);
        }
    }
}
